package com.hnkywl.sts.dao.jhgl;

import com.hnkywl.sts.entity.jhgl.Bgfkjh;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.sql.*;
import java.sql.Date;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-6-29
 * Time: 上午11:26
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class BgfkjhDao extends SimpleHibernateTemplate<Bgfkjh, Long> {
    public Map<String, Object> findBgfkjh(Integer nf, Integer yf, Long gsId, Long gysId, Integer pageNo, Integer pageSize) {
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        int total = 0;
        String issuccess = null;
        String message = null;
        if (gsId == null) gsId = -1L;
        if (gysId == null) gysId = -1L;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call PROC_FKJH(?,?,?,?,?,?,?,?,?,?,?)}");
            call.setInt(1, nf);
            call.setInt(2, yf);
            call.setDate(3, new Date(Calendar.getInstance().getTimeInMillis()));
            call.setLong(4, gsId);
            call.setLong(5, gysId);
            call.setInt(6, (pageNo - 1) * pageSize);
            call.setInt(7, pageNo * pageSize);
            call.registerOutParameter(8, OracleTypes.NUMBER);
            call.registerOutParameter(9, oracle.jdbc.OracleTypes.CURSOR);
            call.registerOutParameter(10, OracleTypes.VARCHAR);
            call.registerOutParameter(11, OracleTypes.VARCHAR);
            call.execute();
            //====================================
            issuccess = ((OracleCallableStatement) call).getString(10);
            message = ((OracleCallableStatement) call).getString(11);
            if (issuccess.equals("0")) {
                throw new RuntimeException(message);
            }
            total = call.getInt(8);
            rs = ((OracleCallableStatement) call).getCursor(9);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, Object> result = new HashMap<String, Object>();
            List<Map> rows = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    if (i < 5)
                        r.put(columnName, rs.getObject(i));
                    else {
                        BigDecimal num = (BigDecimal) rs.getObject(i);
                        r.put(columnName, num);
//                        if (num != null)
//                            r.put(columnName, num.floatValue());
//                        else
//                            r.put(columnName, null);
                    }
                }
                rows.add(r);
            }
            result.put("total", total);
            result.put("rows", rows);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }

    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }

    public List<Bgfkjh> findBgfkjh(Long gsId, Long gysId, Integer zftype) {
        String hql = "from Bgfkjh where nbgs.id=? and gys.id = ? and zftype = ?";
        return find(hql, gsId, gysId, zftype);
    }
}
